//
//  ListTableViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "ListTableViewController.h"
#import "LeftModelCell.h"
#import "LeftModel.h"
#import "DetailViewController.h"

#define category_list_url @"http://api.app.happyjuzi.com/article/list/category"
#define kol_list_url @"http://api.app.happyjuzi.com/article/list/author"
#define hos_url @"http://api.app.happyjuzi.com/foundpage/list/hotnotice"
@interface ListTableViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSString *url;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation ListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.navigationItem.title = self.navTitle;
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"LeftModelCell" bundle:nil] forCellReuseIdentifier:@"LeftModel"];
    
    if ([self.type isEqualToString:@"精选栏目"]) {
        self.url = category_list_url;
    }else if ([self.type isEqualToString:@"媒体号"]){
        self.url = kol_list_url;
    }else {
        self.url = hos_url;
        self.navigationItem.title = @"今日热榜";
    }
    
    [self refreshData];
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {//日间模式
//        self.tableView.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:0.7];
    }
    [self.tableView reloadData];
}


- (void)loadDataFromServerWithPage:(NSInteger)page Url:(NSString *)url {
    NSDictionary *parmaDict = @{@"uid":@"3971092394255935",@"res":@"1080x1920",@"ver":@"2.11",@"pf":@"android",@"channel":@"yingyongbao",@"accesstoken":@"e43c576b3de11eeeb758223cb56752c1",@"page":@(page),@"id":@(self.ID),@"net":@"wifi",@"ts":@"0"};
    [[AFHTTPSessionManager manager]GET:url parameters:parmaDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (page == 1) {
            [self.dataSource removeAllObjects];
        }
        
        NSDictionary *dataDict = responseObject[@"data"];
        for (NSDictionary *dict in dataDict[@"list"]) {
            LeftModel *model = [LeftModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSource addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
   
}


//添加刷新
- (void)refreshData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
//获取新数据
- (void)loadNewData {
    self.page = 1;
    [self loadDataFromServerWithPage:self.page Url:self.url];
}
//加载更多数据
- (void)loadMoreData {
    self.page++;
    [self loadDataFromServerWithPage:self.page Url:self.url];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftModelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftModel"];
    
    cell.model = self.dataSource[indexPath.row];
    
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [cell updateToNightSkinMode];
    }else {
        [cell updateToDaySkinMode];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftModel *model = self.dataSource[indexPath.row];
    NSString *urlRoute = [model.urlroute componentsSeparatedByString:@"?"][0];
    if ([urlRoute isEqualToString:@"juzi://specialreport"]) {
        return;
    }
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    detailVC.ID  = model.ID;
    detailVC.listmodel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.view.window == nil && [self isViewLoaded]) {
        self.view = nil;
    }
}
@end
